package com.webserver.core;

import com.webserver.http.HttpRequest;

import java.io.*;
import java.net.Socket;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处理与指定客户端的交互
 * HTTP协议要求客户端与服务端的交互采取一问一答的原则
 * 因此交互流程分为三步
 * 1.解析请求（读取客户端发过来的请求）
 * 2.处理请求
 * 3.发送响应
 */
public class ClientHandler implements Runnable{
    private Socket socket;
    public ClientHandler(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        try{
            //1.解析请求
            HttpRequest request=new HttpRequest(socket);
            //2.处理请求
            //3响应客户端
            /*
            给客户端发送一个标准的HTTP响应,将webapps/myweb/index.html发送给客户端
            HTTP/1.1 200 OK(CRLF)
            Content-Type: text/html(CRLF)
            Content-Length: 2546(CRLF)(CRLF)
            1011101010101010101......
             */
            //通过request获取浏览器请求的抽象路径
            String path=request.getUri();
            System.out.println("浏览器请求的抽象路径为："+path);
            File file=new File("./webapps"+path);
            File file1=new File("./webapps/root/404.notfound.html");
            OutputStream out=socket.getOutputStream();
            FileInputStream fis;
            String line;
            //判断该文件是否存在并要求是一个文件
            if (file.exists()&&file.isAbsolute()){
                //1.发送状态行
                line="HTTP/1.1 200 OK";
                out.write(line.getBytes("iso8859-1"));
                out.write(13);
                out.write(10);
                line="Content-Type: text/html";
                out.write(line.getBytes("iso8859-1"));
                out.write(13);
                out.write(10);
                line="Content-Length: "+file.length();
                out.write(line.getBytes("iso8859-1"));
                out.write(13);
                out.write(10);

                out.write(13);
                out.write(10);
                 fis=new FileInputStream(file);
            }else {
                line="HTTP/1.1 404 NotFound";
                out.write(line.getBytes("ISO8859-1"));
                out.write(13);
                out.write(10);
                line="Content-Type: text/html";
                out.write(line.getBytes("iso8859-1"));
                out.write(13);
                out.write(10);
                line="Content-Length: "+file1.length();
                out.write(line.getBytes("iso8859-1"));
                out.write(13);
                out.write(10);

                out.write(13);
                out.write(10);
                fis=new FileInputStream(file1);
            }

            //发送响应正文

            byte[]data=new byte[1024*10];
            int len;
            while ((len=fis.read(data))!=-1)
                out.write(data,0,len);
            System.out.println("发送完毕！");

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                socket.close();//处理交互后与客户端断开联系
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}
